/**
 * 
 */
package com.taobao.snowsnow.hibernate.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;

import com.taobao.snowsnow.hibernate.dao.ProductTradeRecordDao;
import com.taobao.snowsnow.hibernate.pojo.ProductCollect;
import com.taobao.snowsnow.hibernate.pojo.ProductTradeRecord;

/**
 * @author Tux
 *
 * 创建时间: 2011-4-29 上午09:45:11
 */
@SuppressWarnings("unchecked")
public class ProductTradeRecordDaoImpl extends BaseDaoImpl implements ProductTradeRecordDao {
	
	public ProductTradeRecord findById(int id) throws Exception {
		return (ProductTradeRecord) getHibernateTemplate().get(ProductTradeRecord.class, id);
	}
	
	public List<ProductTradeRecord> findByName(String name) throws Exception {
		return super.findByProperty(ProductTradeRecord.class, "name", name);
	}

	public List<ProductTradeRecord> findAll() throws Exception {
		return getHibernateTemplate().find("from ProductTradeRecord");
	}

	public long getTotal() throws Exception {
		return super.countByCriteria(ProductTradeRecord.class);
	}

	public List<ProductTradeRecord> paging(int page, int limit) throws Exception {
		return super.findByCriteriaByPage(ProductTradeRecord.class, page, limit);
	}

	@Override
	public ProductTradeRecord getFirstObject() throws Exception {
		List<ProductTradeRecord> list = getHibernateTemplate().find("from ProductTradeRecord as o where o.id = (select min(id) from ProductTradeRecord)");
		if(list.size() !=0)
			return (ProductTradeRecord) list.get(0);
		else return null;
	}

	@Override
	public ProductTradeRecord getLastObject() throws Exception {
		List<ProductTradeRecord> list = getHibernateTemplate().find("from ProductTradeRecord as o where o.id = (select max(id) from ProductTradeRecord)");
		if(list.size() !=0)
			return (ProductTradeRecord) list.get(0);
		else return null;
	}

	@Override
	public ProductTradeRecord findByNamePrecise(String name) throws Exception {
		List<ProductTradeRecord> list = findByName(name);
		if(list.size() > 0)
			return list.get(0);
		return null;
	}

	@Override
	public ProductTradeRecord findBySellDateAndProductCollect(Date date, ProductCollect product)
			throws Exception {
		
		List<ProductTradeRecord> list = getHibernateTemplate().findByNamedParam("from ProductTradeRecord as o where o.sellDate = :sellDate and o.productCollect = :productCollect", new String[]{ "sellDate", "productCollect" }, new Object[]{ date, product });
		if(list.size() !=0)
			return (ProductTradeRecord) list.get(0);
		else return null;
	}
}
